package com.shuqi.model;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.text.TextUtils;

import com.shuqi.application.PasswordProcess;
import com.shuqi.common.ConfigVersion;
import com.shuqi.common.MyTask;
import com.shuqi.common.Urls;
import com.shuqi.common.utils.RegexUtil;
import com.shuqi.common.utils.Util;
import com.shuqi.model.net.HttpRequest;
import com.shuqi.model.net.UrlManager;
import com.shuqi.model.net.listener.HttpRequestListener;
import com.shuqi.model.parse.parser.LoginAccountParser;

public class LoginAccountModel extends ModelBase implements HttpRequestListener {

    private HttpRequest mHttpRequest;

    private Context mContext;

    private String mAccount;

    private String mPassword;

    public LoginAccountModel(Context context) {
        this.mContext = context;
    }

    @Override
    public void loadData(Object... objs) {
        // TODO Auto-generated method stub
        mAccount = (String) objs[0];
        mPassword = (String) objs[1];

        mHttpRequest =
                new HttpRequest(mContext, UrlManager.COLUMN_DOMAIN_ACCOUNT, getRequestUrl(), setParams(),
                        this);
        mHttpRequest.setDataParseAdapter(new LoginAccountParser());
        MyTask.runInBackground(mHttpRequest, true);
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        if (mHttpRequest != null) {
            mHttpRequest.abort();
        }
    }

    @Override
    public void action(int actionCode, Object object) {
        // TODO Auto-generated method stub
        fireEvent(actionCode, object);
    }

    @Override
    public List<BasicNameValuePair> setParams() {
        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
        params.add(new BasicNameValuePair("mobile", mAccount));
        if (!TextUtils.isEmpty(mPassword)) {
            params.add(new BasicNameValuePair("pwd", PasswordProcess.getLgPassword(mPassword)));
        }
        params.add(new BasicNameValuePair("forwhere", "sf"));// TODO
        params.add(new BasicNameValuePair("imei", ConfigVersion.IMEI));
        params.add(new BasicNameValuePair("sn", ConfigVersion.SN));
        if (RegexUtil.isEmail(mAccount)) {
            params.add(new BasicNameValuePair("type", "2"));
        } else {
            params.add(new BasicNameValuePair("type", "1"));
        }
        String key = "c56cf32e9a52a265ae47cd50570266cc";
        long timestamp = System.currentTimeMillis();
        String strTimestamp = timestamp + "";
        String md5_key_sb = key + timestamp;
        String md5_key = Util.MD5(md5_key_sb.toString());

        params.add(new BasicNameValuePair("md5_key", md5_key));// TODO
        params.add(new BasicNameValuePair("timestamp", strTimestamp));
        return params;
    }

    @Override
    public String getRequestUrl() {
        // TODO Auto-generated method stub
        return Urls.getLoginAccount();
    }

    public void loginBySina(String uid) {

        String key = "c56cf32e9a52a265ae47cd50570266cc";
        long timestamp = System.currentTimeMillis();
        String strTimestamp = timestamp + "";
        String md5_key_sb = key + timestamp;
        String md5_key = Util.MD5(md5_key_sb.toString());
        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
        params.add(new BasicNameValuePair("tpid", uid));
        params.add(new BasicNameValuePair("tptype", "1"));
        params.add(new BasicNameValuePair("key", md5_key));// md5
        params.add(new BasicNameValuePair("imei", ConfigVersion.IMEI));
        params.add(new BasicNameValuePair("sn", ConfigVersion.SN));
        params.add(new BasicNameValuePair("timestamp", strTimestamp));// 时间戳

        mHttpRequest =
                new HttpRequest(mContext, UrlManager.COLUMN_DOMAIN_ACCOUNT, Urls.getLoginByThirdUrl(),
                        params, this);
        mHttpRequest.setDataParseAdapter(new LoginAccountParser());
        MyTask.runInBackground(mHttpRequest, true);
    }
}
